﻿using LucyGrid;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LucyGridTest
{
	class TestHelper
	{
		public TestHelper(TestContext tc) {
			TestContext = tc;
		}
		private TestContext testContextInstance;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}
			set
			{
				testContextInstance = value;
			}
		}

		public class GridNodes : IDisposable {

			Task[] tasks;
			public GridNodes(Task[] t) {
				tasks = t;
			}
			public void Dispose()
			{
				new GridComm().EmitStopAll();
				Task.WaitAll(tasks);
			}
		}

		public GridNodes StartGridNode(int nodes)
		{
			return new GridNodes (Enumerable.Range(0, nodes)
				.Select(i =>
						Task.Run(() =>
						{
							var file = Directory.GetFiles(Path.GetFullPath(TestContext
									.TestRunDirectory + "../../../LucyGridNode/bin/debug")).First(f => f.EndsWith(".exe"));
							var p = Process.Start(new ProcessStartInfo()
							{
								FileName = file
							});
							p.WaitForExit();
						})).ToArray());
		}
	}
}
